/**
 *查询行政区下拉框
 */
'use strict';

const conn = require('../../libs/DB/Conn');
const _ = require("underscore");

module.exports = async function (params) {
   let downDropList = await conn(`
       SELECT 
            district_id AS value,
            district_name AS label,
            parent_id AS parentId
       FROM
          district_file 
       WHERE 
          delete_sign = 1           
   `);
   let dataList = childrenListSort(0,downDropList,0)
   return {success:true,downDropList:dataList}
};
function childrenListSort(value,downDropList,level){
   level++;
   let childrenList = [];
   let dataList = _.filter(downDropList,{parentId:value})
   for(const childIndex in dataList){
      let children = childrenListSort(dataList[childIndex].value,downDropList,level);
      if(children.length > 0){
         dataList[childIndex].children = children;
      }
      if(level > 2 || (level <=2 && dataList[childIndex].children)){
         childrenList.push(dataList[childIndex]);
      }
   }
   return childrenList;
}